/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.UsertypeModuleDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.UsertypeModule;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Dean
 */
@Results({
    @Result(name = "error", type = "redirectAction", location = "/redirect-page", params = {"url", "/WEB-INF/error/messages.jsp"}),
    @Result(name = "success", location = "${pagename}")
})
public class QueryPrivilege2 extends ActionSupport {

    private Logger logger = Logger.getLogger(QueryPrivilege.class);
    private String pagename;
    private String moduleid;
    private boolean createable, viewable, deleteable, editable;
    private UsertypeModuleDAO umodDao = (UsertypeModuleDAO) ServletActionContext.getServletContext().getAttribute("usermoduleDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/query-privilege2")
    @Override
    public String execute() throws Exception {
        try {
            this.queryPrivilege(Integer.parseInt(moduleid), ut.getUsertypeid());
            return SUCCESS;
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);

            }
            e.printStackTrace();
            //add action errors this will correspond to <s:actionerror/> in a struts-enabled jsp page.
            addActionError(e.getLocalizedMessage());

            //if error then chain result to messages.jsp, which will display errors/messages.
            return ERROR;
        } finally {
            UsertypeModuleDAO.close();
        }
    }

    public void queryPrivilege(int moduleid, int usertypeid) throws Exception {
        DetachedCriteria criteria = DetachedCriteria.forClass(UsertypeModule.class);
        criteria.createCriteria("usertypes", "ut").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.createCriteria("modules", "m").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.add(Restrictions.eq("m.moduleid", moduleid));
        criteria.add(Restrictions.eq("ut.usertypeid", usertypeid));
        UsertypeModule um = umodDao.getPrivilege(criteria);
        createable = um.isCreateable();
        viewable = um.isViewable();
        deleteable = um.isDeleteable();
        editable = um.isEditable();
    }

    public void setModuleid(String moduleid) {
        this.moduleid = moduleid;
    }

    public void setPagename(String pagename) {
        this.pagename = pagename;
    }

    public String getPagename() {
        return pagename;
    }

    public boolean isCreateable() {
        return createable;
    }

    public boolean isDeleteable() {
        return deleteable;
    }

    public boolean isEditable() {
        return editable;
    }

    public boolean isViewable() {
        return viewable;
    }
}